package hw;


import java.util.Scanner;

/**
 * 输入整型数组和排序标识，对其元素按照升序或降序进行排序
 *
 * 数据范围： 1 \le n \le 1000 \1≤n≤1000  ，元素大小满足 0 \le val \le 100000 \0≤val≤100000
 * 输入描述：
 * 第一行输入数组元素个数
 * 第二行输入待排序的数组，每个数用空格隔开
 * 第三行输入一个整数0或1。0代表升序排序，1代表降序排序
 *
 * 输出描述：
 * 输出排好序的数字
 *
 */

public class hj101 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()){
            String s = in.nextLine();
            String s1 = in.nextLine();
            String s2 = in.nextLine();
            int size = Integer.parseInt(s);
            int sort = Integer.parseInt(s2);
            String[] strings = s1.split(" ");
            int[] nums = new int[size];
            for (int i = 0; i < strings.length; i++) {
                nums[i] = Integer.parseInt(strings[i]);
            }
            //快排
            quickSort(nums, 0, size-1);
            StringBuilder stringBuilder = new StringBuilder();
            if (sort == 0){
                for (int i = 0; i < nums.length; i++) {
                    stringBuilder.append(nums[i]).append(" ");
                }
            }else {
                for (int i = nums.length - 1; i >= 0; i--) {
                    stringBuilder.append(nums[i]).append(" ");
                }
            }
            System.out.println(stringBuilder.substring(0, stringBuilder.length()-1));
        }
    }

    public static void quickSort(int[] nums, int left, int right){
        if (left > right){
            return;
        }
        int mid = nums[left];
        int i=left,j=right;
        while (i != j){
            while (i<j && nums[j] >= mid){
                j--;
            }
            while (i<j && nums[i] <= mid){
                i++;
            }
            if (i<j){
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
        nums[left] = nums[i];
        nums[i] = mid;

        quickSort(nums, left, i-1);
        quickSort(nums, j+1, right);
    }
}
